package com.vecharm.app.ui.cloudshelf

import android.os.Bundle
import android.view.View
import android.view.WindowManager
import com.google.gson.Gson
import com.ling.sdk.ui.BaseFragment
import com.ling.sdk.utils.dip
import com.vecharm.app.R
import com.vecharm.app.bean.*
import com.vecharm.app.ui.BaseShowActivity
import com.vecharm.app.ui.smartlable.IAction
import com.vecharm.app.ui.smartlable.VideoFragment
import com.vecharm.app.ui.web.WebFragment
import com.vecharm.app.utils.QRCodeUtil
import com.vecharm.app.utils.TOKEN
import com.vecharm.app.utils.url
import kotlinx.android.synthetic.main.activity_goods_detail.*
import java.lang.Exception

class GoodsDetailActivity : BaseShowActivity() {


    override fun getLayoutId() = R.layout.activity_goods_detail


    override fun onInitView() {
        window.addFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED)
        super.onInitView()
        dataToken = intent.getStringExtra(TOKEN)
        try {
            val goods: Goods = Gson().fromJson(dataToken, Goods::class.java)
            val fragment = getFragment(goods)
            loadRootFragment(R.id.container, fragment)
            (fragment as? IAction)?.onStartPlay()
            goods.qRCodeUrl?.also { QRCodeUtil.createQRCodeBitmap(it, 100.dip()).apply { qrcord.also { it.visibility = View.VISIBLE }.setImageBitmap(this) } }
        } catch (e: Exception) {
            finish()
            return
        }

    }


    private fun getFragment(goods: Goods): BaseFragment {
        return when (goods.type) {
            IMAGES -> BannerDetailFragment().setGoods(goods)
            VIDEO -> VideoFragment().setList(goods)
            else -> WebFragment().setList(goods)
        }
    }

    private fun BaseFragment.setList(goods: Goods): BaseFragment {
        val bundle = Bundle()
        bundle.putStringArrayList("list", goods.showUrl)
        arguments = bundle
        return this
    }

    private fun BaseFragment.setGoods(goods: Goods): BaseFragment {
        val bundle = Bundle()
        bundle.putSerializable("goods", goods)
        arguments = bundle
        return this
    }


}